package com.apes.fn.crm.ReturnVisit.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.fn.crm.ReturnVisit.model.VisitCustomerExpand;
import com.apes.fn.crm.ReturnVisit.repository.VisitCustomerExpandRepository;
import com.apes.fn.crm.customer.FnCustomer;
import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import com.apes.framework.util.Tools;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.enumeration.repository.EnumerationRepository;
import com.apes.scm.masterdata.party.model.AccessProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service("visitCustomerExpandService")
public class VisitCustomerExpandService extends DomainService {

    @Autowired
    VisitCustomerExpandRepository visitCustomerExpandRepository;
    @Autowired
    EnumerationRepository enumerationRepository;

    /***
     * 创建
     */
    public VisitCustomerExpand create(SimpleRequest request) {
        JSONObject tempJson = request.getJO();
        VisitCustomerExpand visitCustomerExpand = visitCustomerExpandRepository.findOne(
                JpaDsl.toCriteriaByEq(
                        "mobile", tempJson.getString("mobile"),
                        "accessProvider.id", tempJson.getJSONObject("customer").getString("accessProviderId")
                )
        ).orElse(null);
        if (visitCustomerExpand != null) {
            visitCustomerExpand.setPrevVisitDate(new Date());
            if (tempJson.get("notVisitSign") != null) visitCustomerExpand.setNotVisitSign(false);
            if (tempJson.get("visitPeriod") != null) {
                Enumeration enumeration = enumerationRepository.findOne(tempJson.getJSONObject("visitPeriod").getString("id"));
                visitCustomerExpand.setVisitPeriod(enumeration);
            }
        } else {
            AccessProvider accessProvider = new AccessProvider();
            accessProvider.setId(tempJson.getJSONObject("customer").getString("accessProviderId"));

            visitCustomerExpand = Tools.map(MapUtil.mapper(
                    "mobile", tempJson.get("mobile"),
                    "notVisitSign", tempJson.get("notVisitSign") != null ? tempJson.getBoolean("notVisitSign") : false,
                    "accessProvider",accessProvider,
                    "visitPeriod", tempJson.get("visitPeriod") != null ? tempJson.get("visitPeriod") : null,
                    "prevVisitDate", new Date()
            ), VisitCustomerExpand.class);
        }
        return visitCustomerExpandRepository.saveAndFlush(visitCustomerExpand);
    }

    /***
     * 修改
     */
    public VisitCustomerExpand update(SimpleRequest request) {
        isValid(request, visitCustomerExpandRepository);
        VisitCustomerExpand visitCustomerExpand = request.getO(VisitCustomerExpand.class);
        return visitCustomerExpandRepository.saveAndFlush(visitCustomerExpand);
    }

    /***
     * 删除
     */
    public String delete(SimpleRequest request) {
        isValid(request, visitCustomerExpandRepository);
        VisitCustomerExpand visitCustomerExpand = request.getO(VisitCustomerExpand.class);
        visitCustomerExpandRepository.delete(visitCustomerExpand);
        return "删除成功！";
    }

    /***
     * 通过客户码查询数据
     */
    public VisitCustomerExpand findByCustomerId(String mobile, String vendorCode) {
        return visitCustomerExpandRepository.findOne(JpaDsl.toCriteriaByEq(
                "mobile", mobile,
                "accessProvider.id", vendorCode
        )).orElse(null);
    }

    /***
     * 数据是否合法
     * @param request
     * @param repository
     */
    private Model isValid(SimpleRequest request, CustomRepository repository) {
        Model oldModel = (Model) repository.findOne((String) request.get("id"));
        if (oldModel == null) throw new RuntimeException("编码【" + request.get("id") + "】在系统中不存在。");
        if (oldModel.getVersion() != (int) request.get("version")) throw new RuntimeException("记录已更新或已删除，请重新提交！");
        return oldModel;
    }
}
